#!/bin/bash -eu
#
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source /opt/c2d/c2d-utils || exit 1
source /opt/mattermost-utils || exit 1

# Default values
readonly default_db_host="localhost"
readonly default_db_name="mattermost"
readonly default_db_user="mattermost"
readonly default_domain="localhost"

# Get settings from metadata
readonly metadata_db_pwd="${metadata_db_pwd:-$(get_attribute_value "mattermost-database-pwd")}"
readonly metadata_sitename="${metadata_sitename:-$(get_attribute_value "mattermost-sitename")}"

# Create user in MySQL database
create_db_user \
  "${default_db_host}" "${default_db_name}" "${default_db_user}" "${metadata_db_pwd}"

# Build Data Source config
database_dsn="${default_db_user}:${metadata_db_pwd}@tcp(${default_db_host}:3306)/${default_db_name}?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s"

# Update DSN and Sitename
update_config "${database_dsn}" "${metadata_sitename}"

# Setup nginx
setup_nginx "${default_domain}"

# Start Mattermost
systemctl start mattermost

# Wait until ready.
wait_for_app
